/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package service.promo.impl;

import dao.promo.PromoDao;
import java.util.List;
import java.util.Map;
import model.promo.Promo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import service.promo.PromoService;
import utility.NameValueList;
import utility.StringUtils;

/**
 *
 * @author jay
 */
@Service("promoService")
@Transactional(readOnly=true)
public class PromoServiceImpl implements PromoService {
    //avoid LazyInitializationException 
    String mainQuery = "select promo from Promo promo";
    
    @Autowired private PromoDao promoDao;

    @Transactional(readOnly=false)
    @Override
    public boolean save(Promo data) {
        return promoDao.save(data);
    }

    @Transactional(readOnly=false)
    @Override
    public boolean delete(Promo data) {
        return promoDao.delete(data);
    }

    @Override
    public Promo getPromo(int id) {
        String query = mainQuery+" where promo.id = "+id;
        return promoDao.getByCustomQuery(query);
    }

   @Override
    public List<Promo> getPromo(int start, int num, Map<NameValueList, Object> params) {
        String query = mainQuery+StringUtils.getFilter(params);
        return promoDao.getAll(start, num, query);
    }
    
    @Override
    public Long count(Map<NameValueList, Object> params) {
        String query= "select count(*) from Promo promo "+StringUtils.getFilter(params);
        return promoDao.count(query);
    }
       
}
